import logging
import platform
import uuid

from loguru import logger

def set_logger():
    # 日志消息输出到文件
    # logging.basicConfig(filename='app.log', level=logging.DEBUG)

    # 127.0.0.1 - - [03/Nov/2023 02:51:01]
    # logger = logging.getLogger(__name__)
    logger = logging.getLogger("softLogger")

    # 日志处理器
    file_handler = logging.FileHandler("example.log")
    file_handler.setLevel(logging.INFO)

    # 再创建一个handler，用于输出到控制台
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)

    # 日志格式化器
    formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)

    # 日志处理器添加到 Logger 对象
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)

    return logger

def set_loguru_logger():
    # 清除之前的设置
    # logger.remove(handler_id=None)

    # format="{time:YYYY-MM-DD HH:mm:ss} {extra[ip]}  {extra[username]} {level} From {module}.{function} : {message}"
    trace = logger.add("file_X.log",

               format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}",
               rotation="500 MB", retention="10 days",
               encoding="utf-8", enqueue=True,
               compression='zip') # 配置文件的压缩格式: compression='zip'

    # trace = logger.add("file_X.log")
    # logger.remove(trace)

    # logger.debug("That's it, beautiful and simple logging!")
    logger.info('If you are using Python {version}, prefer {feature} of course!',
                version=platform.python_version(),
                feature='f-strings')

    # logger.debug('This is debug information')
    # logger.info('This is info information')
    # logger.warning('This is warn information')
    # logger.error('This is error information')

    return logger


if __name__ == '__main__':
    logger = set_loguru_logger()
    logger.info(uuid.uuid1())

